<template>
  <view class="custom-nav-bar" :style="{ paddingTop: statusBarHeight + 'px' }">
    <view class="nav-content">
      <view class="nav-left" v-if="showBack" @click="goBack">
        <text class="back-icon">←</text>
      </view>
      <view class="nav-title">
        <text class="title-text">{{ title }}</text>
      </view>
      <view class="nav-right">
        <slot name="right"></slot>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'

interface Props {
  title: string
  showBack?: boolean
}

withDefaults(defineProps<Props>(), {
  showBack: false,
})

const statusBarHeight = ref(0)

const goBack = () => {
  uni.navigateBack({
    delta: 1,
  })
}

onMounted(() => {
  let statusBar = 0
  if (uni.getWindowInfo) {
    statusBar = uni.getWindowInfo().statusBarHeight || 0
  } else {
    statusBar = uni.getSystemInfoSync().statusBarHeight || 0
  }
  statusBarHeight.value = statusBar
})
</script>

<style scoped>
.custom-nav-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: #ffffff;
  z-index: 1000;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
}

.nav-content {
  height: 88rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 30rpx;
}

.nav-left {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-icon {
  font-size: 40rpx;
  color: #333;
  font-weight: bold;
}

.nav-title {
  flex: 1;
  text-align: center;
}

.title-text {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

.nav-right {
  width: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
